
var EventType = cc.Enum({
    START_SCROLL : 0,
    SCROLLING : 1,
    END_SCROLL : 2
})

cc.Class({
    name:'EllipseScroll',
    extends: cc.Component,

    properties: {
        nodes : {
            default :[],
            type : require('EllipseSprite')
        },
        longLine : 100,
        shortLine : 100,
        sensitive:{
            type : cc.Float,
            default : 2,
            toolTip : '灵敏度'
        },
        topScale :{
            type : cc.Integer,
            default : 1,
            toolTip : '顶部的缩放'
        },
        topOpacity :{
            type : cc.Float,
            default : 150,
            toolTip : '顶部的透明度'
        },
        changeEvents : {
            "default": [],
            type: cc.Component.EventHandler,
            tooltip: "i18n:COMPONENT.button.click_events"
        },
        clickEvents : {
            "default": [],
            type: cc.Component.EventHandler,
            tooltip: "i18n:COMPONENT.button.click_events"
        }
    },

    statics:{
        EventType : EventType
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.angle = 0;
        this.runTime = 0;
        this.lastAngle = 0;
        this.touchAngle = 0;
        this.allTime = 0.3;
        this.node.on(cc.Node.EventType.TOUCH_MOVE,this.onTouch,this);
        this.node.on(cc.Node.EventType.TOUCH_END,this.onTouch,this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL,this.onTouch,this);
    },

    onTouch(event){
        var start = event.getStartLocation();
        var current = event.getLocation();
        if(event.type == cc.Node.EventType.TOUCH_MOVE){
            let dx = current.x - start.x;
            this.touchAngle = dx/2
            for(var i=0; i<this.nodes.length; i++){
                var node = this.nodes[i]
                node.setAngle((node._angle + this.touchAngle+360)%360)
            }
            cc.Component.EventHandler.emitEvents(this.changeEvents, {type:EventType.SCROLLING});
        }else {
            let dp = event.getLocation().sub(event.getStartLocation())
            // console.log('dp:',dp)
            if(Math.abs(dp.x) < 10 && Math.abs(dp.y) < 10){
                cc.Component.EventHandler.emitEvents(this.clickEvents);
                return;
            }
            SoundMgr.playSound('Audio/button_action')
            this.lastAngle = (this.touchAngle+360)%360

            var array = [];
            for(var i=0; i<this.nodes.length; i++){
                array.push(360/this.nodes.length*i)
            }
            for(var i =0; i<array.length; i++){
                if(Math.abs(this.lastAngle-array[i]) < 360/this.nodes.length){
                    this.angle = array[i]
                }
            }
            this.isMove = true;
            this.allTime = 0.1;
            this.runTime = 0;
        }
    },

    start(){
        this.initAngle();
    },

    initAngle(){
        for(var i=0; i<this.nodes.length; i++){
            var node = this.nodes[i]
            var angle = (270-360/this.nodes.length*i+360)%360
            node.setOptions({id:i,x:0,y:0,a:this.longLine,b:this.shortLine,angle})
            node.setTopScale(this.topScale)
            node.setTopOpacity(this.topOpacity)
        }
    },

    scrollTo(idx){
        this.runTime = 0;
        this.lastAngle = 0
        this.angle = idx*90
        this.allTime = 0.3
        this.isMove = true;
        for(var i=0; i<this.nodes.length; i++){
            var node = this.nodes[i]
            node.setAngle(node.angle + idx*90)
        }
    },

    onClickScroll(event,data){
        if(this.isMove){
            return;
        }
        this.scrollTo(parseInt(data));
        cc.Component.EventHandler.emitEvents(this.changeEvents, {type:EventType.START_SCROLL});
    },

    update (dt) {
        if(!this.isMove){
            return
        }
        this.runTime +=dt;
        var angle = this.lastAngle += (this.angle-this.lastAngle)*(this.runTime)/this.allTime
        for(var i=0; i<this.nodes.length; i++){
            var node = this.nodes[i]
            node.setAngle((node._angle + angle+360)%360)
        }
        if(this.runTime >= this.allTime){
            this.isMove = false;
            this.lastAngle = this.angle % 360
            let idx = -1;
            for(var i=0; i<this.nodes.length; i++){
                var node = this.nodes[i]
                node._angle = (node._angle + angle+360)%360
                if(Math.abs(node._angle -270) < 5){
                    idx = i;
                }
            }
            if(idx == -1){
                this.initAngle();
                idx= 0
            }
            cc.Component.EventHandler.emitEvents(this.changeEvents, {type:EventType.END_SCROLL,idx:idx});
        }
    },
});
